package com.starry.module.system.core.tenant.controller.admin;

import cn.hutool.http.HttpStatus;
import com.starry.core.common.domain.R;
import com.starry.core.common.domain.vo.PageVo;
import com.starry.core.tenant.core.aop.TenantIgnore;
import com.starry.core.web.core.controller.BaseController;
import com.starry.module.system.api.tenant.domain.qo.SysTenantPageQo;
import com.starry.module.system.api.tenant.domain.qo.SysTenantQo;
import com.starry.module.system.api.tenant.domain.vo.SysTenantDtlVo;
import com.starry.module.system.api.tenant.domain.vo.SysTenantVo;
import com.starry.module.system.core.tenant.service.SysTenantService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

/**
 * 系统租户
 *
 * @author xiaoke
 */
@Tag(name = "系统租户")
@RestController
@RequestMapping("/system/sysTenant")
public class SysTenantController extends BaseController {

    private final SysTenantService sysTenantService;

    public SysTenantController(SysTenantService sysTenantService) {
        this.sysTenantService = sysTenantService;
    }

    /**
     * 分页查询
     *
     * @param pageQo 查询实体
     * @return 所有数据
     */
    @Operation(summary = "系统租户-分页查询")
    @GetMapping("sysTenantPage")
    public R<PageVo<SysTenantVo>> findPage(SysTenantPageQo pageQo) {
        return success(this.sysTenantService.page(pageQo));
    }

    /**
     * 详情
     *
     * @param id 主键
     * @return 单条数据
     */
    @Operation(summary = "系统租户-详情")
    @GetMapping("sysTenantDetails/{id}")
    public R<SysTenantDtlVo> findById(@PathVariable("id") Long id) {
        return success(this.sysTenantService.getById(id));
    }

    /**
     * 新增数据
     *
     * @param sysTenantQo 实体对象
     * @return 新增结果
     */

    @Operation(summary = "系统租户-新增")
    @PostMapping("sysTenantCreate")
    public R<Long> insert(@RequestBody @Validated SysTenantQo sysTenantQo) {
        return success(HttpStatus.HTTP_CREATED, this.sysTenantService.save(sysTenantQo));
    }

    /**
     * 修改数据
     *
     * @param sysTenantQo 实体对象
     * @return 修改结果
     */
    @TenantIgnore
    @Operation(summary = "系统租户-修改")
    @PostMapping("sysTenantUpdate")
    public R<Void> update(@RequestBody @Validated SysTenantQo sysTenantQo) {
        this.sysTenantService.updateById(sysTenantQo);
        return success();
    }

    /**
     * 删除数据
     *
     * @param idList 主键集合
     * @return Void
     */
    @TenantIgnore
    @Operation(summary = "系统租户-删除")
    @PostMapping("sysTenantRemove")
    public R<Void> delete(@RequestParam("idList") Set<Long> idList) {
        this.sysTenantService.removeByIds(idList);
        return success();
    }

    /**
     * 启用数据
     *
     * @param idList 主键集合
     * @return Void
     */
    @TenantIgnore
    @Operation(summary = "系统租户-启用")
    @PostMapping("sysTenantEnable")
    public R<Void> enable(@RequestParam("idList") Set<Long> idList) {
        this.sysTenantService.enable(idList);
        return success();
    }

    /**
     * 禁用数据
     *
     * @param idList 主键集合
     * @return Void
     */
    @TenantIgnore
    @Operation(summary = "系统租户-禁用")
    @PostMapping("sysTenantDisable")
    public R<Void> disable(@RequestParam("idList") Set<Long> idList) {
        this.sysTenantService.disable(idList);
        return success();
    }
}

